import {
  queryDivisionWater,
  queryDivisionCopyrate,
  querySumYearPay,
  querySumCurrentMonthPay,
  queryIncoming,
  queryWaterUse,
  queryStationList,
} from '../services/index';

export default {
    namespace: 'business',
  
    state: {
      IncomingResource:{
        depositItems: [], // 用水情况分析
        useTypeItems: [], // 缴费来源分析
      },
      WaterUseResource: [], // 催缴水费分析
      DivisionWaterResource: [], // 产销情况分析
      DivisionCopyrateResource: [], // 抄表情况分析
      sumYearPayResource: [
        // {
        //   "organizationId": 16,
        //   "billDate": "2019-10-01",

        //   "totalAmount": 4200693400,
        //   "paidAmount": 2865573200,
        //   "unpayAmount": 1335120200
        // },
      ], // 收费分析

      IndicatorsDivisionWaterResource: [],
      IndicatorsDivisionCopyrateResource: [],
      IndicatorsCurrentMonthPayResource: [
        // { "billDate":"2019-10-01","totalAmount":4768390700,"paidAmount":3099077700,"unpayAmount":1669313000 },
        // { "billDate":"2019-09-01","totalAmount":6059728800,"paidAmount":3601464600,"unpayAmount":2458264200 },
        // null
      ],
      // 动态指标
      DynamicIndicatorsDivisionWaterResource: [
        // {
        //     "waterSell": 165,
        //     "time": 1569859200,
        //     "waterSupply": 476
        // },
      ],
      DynamicIndicatorsDivisionCopyrateResource: [
        // {
        //   "billDate":"2019-10-01",
        //   "totalAmount":4768390700,
        //   "paidAmount":3099513300,
        //   "unpayAmount":1668877400
        // }
      ]
    },
  
    effects: {
      // 供水量
      *querySupplyWater({ payload: params }, { call, put }) {
          const { type, ...payload } = params;
        const data = yield call(queryStationList, payload)
        const { success, message, status, ...other } = data
        if (success) {
          yield put({
            type: 'setState',
            payload: {
              [`SupplyWaterResource${type}`]: other.Response,
            },
          })
        } else {
          throw data
        }
      },
      // 售水量
      *querySellWater({ payload: params }, { call, put }) {
        const { type, ...payload } = params;
        const data = yield call(queryWaterUse, payload)
        const { success, message, status, ...other } = data
        if (success) {
          yield put({
            type: 'setState',
            payload: {
              [`SellWaterResource${type}`]: other.data,
            },
          })
        } else {
          throw data
        }
      },
      /** 已收水费 */
      *queryIncomingModel({ payload: params }, { call, put }) {
        const { type, ...payload } = params;
        const data = yield call(queryIncoming, payload)
        const { success, message, status, ...other } = data
        if (success) {
          yield put({
            type: 'setState',
            payload: {
              [`IncomingResource${type}`]: other.data,
            },
          })
        } else {
          throw data
        }
      },
      // 重点指标监控 - 抄表块数、抄见率
      *queryDivisionCopyrate({ payload: params }, { call, put }) {
        const { type, ...payload } = params;
        const data = yield call(queryDivisionCopyrate, payload)
        const { success, message, status, ...other } = data
        if (success) {
          yield put({
            type: 'setState',
            payload: {
              [`DivisionCopyrateResource${type}`]: other.Response[0],
            },
          })
        } else {
          throw data
        }
      },

      // 重点指标监控 - 供水量、售水量、产销差
      *queryIndicatorsDivisionWater({ payload }, { call, put }) {
        const data = yield call(queryDivisionWater, payload)
        const { success, message, status, ...other } = data
        if (success) {
          yield put({
            type: 'setState',
            payload: {
              IndicatorsDivisionWaterResource: other.Response[0].timeList,
            },
          })
        } else {
          throw data
        }
      },
      // 重点指标监控 - 已收水费
      *queryIndicatorsCurrentMonthPay({ payload }, { call, put }) {
        const data = yield call(querySumCurrentMonthPay, payload)
        const { success, message, status, ...other } = data
        if (success) {
          yield put({
            type: 'setState',
            payload: {
              IndicatorsCurrentMonthPayResource: other.data,
            },
          })
        } else {
          throw data
        }
      },
      // 报表分析 - 供水量、售水量、产销差
      *queryDynamicIndicatorsDivisionWater({ payload }, { call, put }) {
        const data = yield call(queryDivisionWater, payload)
        const { success, message, status, ...other } = data
        if (success) {
          yield put({
            type: 'setState',
            payload: {
              DynamicIndicatorsDivisionWaterResource: other.Response[0].timeList,
            },
          })
        } else {
          throw data
        }
      },
      // 报表分析 - 已收水费
      *queryDynamicIndicatorsDivisionCopyrate({ payload }, { call, put }) {
        const data = yield call(querySumYearPay, payload)
        const { success, message, status, ...other } = data
        if (success) {
          yield put({
            type: 'setState',
            payload: {
              DynamicIndicatorsDivisionCopyrateResource: other.data,
            },
          })
        } else {
          throw data
        }
      },
      // 业务分析
      *queryDivisionWater({ payload }, { call, put }) {
        const data = yield call(queryDivisionWater, payload)
        const { success, message, status, ...other } = data
        if (success) {
          yield put({
            type: 'setState',
            payload: {
              DivisionWaterResource: other.Response[0],
            },
          })
        } else {
          throw data
        }
      },
      // *queryDivisionCopyrate({ payload }, { call, put }) {
      //   const data = yield call(queryDivisionCopyrate, payload)
      //   const { success, message, status, ...other } = data
      //   if (success) {
      //     yield put({
      //       type: 'setState',
      //       payload: {
      //         DivisionCopyrateResource: other.Response[0],
      //       },
      //     })
      //   } else {
      //     throw data
      //   }
      // },
      *querySumYearPay({ payload }, { call, put }) {
        const data = yield call(querySumYearPay, payload)
        const { success, message, status, ...other } = data
        if (success) {
          yield put({
            type: 'setState',
            payload: {
              sumYearPayResource: other.data,
            },
          })
        } else {
          throw data
        }
      },
      *queryIncoming({ payload }, { call, put }) {
        const data = yield call(queryIncoming, payload)
        const { success, message, status, ...other } = data
        if (success) {
          yield put({
            type: 'setState',
            payload: {
              IncomingResource: other.data,
            },
          })
        } else {
          throw data
        }
      },
      *queryWaterUse({ payload }, { call, put }) {
        const data = yield call(queryWaterUse, payload)
        const { success, message, status, ...other } = data
        if (success) {
          yield put({
            type: 'setState',
            payload: {
              WaterUseResource: other.data,
            },
          })
        } else {
          throw data
        }
      }
    },
  
    reducers: {
      setState(state, action) {
        return {
            ...state,
            ...action.payload
        };
      }
    },
  }
  